package com.pashanhoo.Thread;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

/**手写reentranlock
 * @author 胡庆安
 * @version V1.0
 * @Package com.pashanhoo.Thread
 * @date 2020/10/22 15:24
 * @Copyright © 2010-2020 爬山虎科技（扬州）股份有限公司
 */
public class HqaLock {
    private static final Unsafe unsafe = reflectGetUnsafe();
    private static final long ownerOffset;

    /**
     * 锁是否被持有(1)是,(0)否
     */
    AtomicInteger state = new AtomicInteger(0);
    /**
     * 当前持有锁的线程
     */
    volatile Thread owner = new Thread();
    /**
     * 线程等待池
     */
    private Queue<Thread> waitQueue = new LinkedBlockingQueue();
    /**
     * 重入次数
     */
    AtomicInteger reentrantCount = new AtomicInteger(0);

    private static Unsafe reflectGetUnsafe() {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            return (Unsafe) field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    static {
        try {
            ownerOffset = unsafe.objectFieldOffset
                    (HqaLock.class.getDeclaredField("owner"));
        } catch (Exception ex) { throw new Error(ex); }
    }
    public void lock(){
        //未被持有则直接获取锁
        if (state.get() == 0){
            //自旋cas改变锁状态
            for(;;){
                boolean b = state.compareAndSet(0,1);
                if (b == true){
                    unsafe.compareAndSwapObject(this,ownerOffset,owner,Thread.currentThread());
                    owner = Thread.currentThread();
                    reentrantCount.set(1);
                    break;
                }
            }

        }else {//已经被持有
            //持有锁的线程是是自己
            if (unsafe.getObject(owner,ownerOffset) == Thread.currentThread()){
                reentrantCount.getAndAdd(1);
            }else {
                waitQueue.add(Thread.currentThread());
                //持有锁的线程不是自己
                while (true){
                    //阻塞休眠
                    LockSupport.park();
                    //如果锁被持有者释放了
                    if (state.get() == 0){
                        owner = Thread.currentThread();
                        state.set(1);
                        break;
                    }
                }
            }
        }
    }

    public void unlock() throws Exception {

        if (unsafe.getObject(this,ownerOffset) != Thread.currentThread()){
            throw new Exception("该线程未持有锁");
        }
        //重入次数减1
        reentrantCount.getAndDecrement();
        //直到重入锁全部解开的才能真正的解锁
        if (reentrantCount.get() == 0){
            state.set(0);
            Thread head = waitQueue.peek();
            LockSupport.unpark(head);
        }

    }
}
